package com.cos.ssm2.web.controller;

import com.cos.ssm2.common.Const;
import com.cos.ssm2.common.ResponseResult;
import com.cos.ssm2.entity.Emp;
import com.cos.ssm2.services.EmpServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author: COS
 * @time: 2022/6/10 9:59
 * @description:
 */
@RestController
@RequestMapping("/emp")
public class EmpController {
    @Autowired
    private EmpServices empServices;

    @GetMapping("/getAll")
    public List<Emp> getAll() {
        return empServices.getAll();
    }

    @GetMapping("/check/{ename}")
    public ResponseResult checkEmpName(@PathVariable("ename") String ename) {
        //访问业务层
        ResponseResult rr = empServices.checkEname(ename);
        return rr;
    }

    @PostMapping("/login")
    public ResponseResult login(String ename, String pwd, HttpSession session) {
        ResponseResult responseResult = empServices.doLogin(ename, pwd);
        if (responseResult.getCode()==1) {
            //登录成功记录数据
            session.setAttribute(Const.IS_LOGIN,responseResult.getData());
        }
        return responseResult;
    }

    @GetMapping("/getEmpByPage")
    public ResponseResult getEmpByPage(Integer page){
        //默认10条数据
        Integer limit=10;
        return empServices.getEmpByPage(page,limit);
    }

    @PostMapping("/del/{eno}")
    public ResponseResult del(@PathVariable("eno") Integer eno){
        //直接放业务层
        return empServices.delEmp(eno);
    }

    @PostMapping("/add")
    public ResponseResult add(Emp e){
        return empServices.addEmp(e);
    }

    @GetMapping("/getEmpByEno/{eno}")
    public ResponseResult getEmpByEno(@PathVariable("eno") Integer eno){
        return empServices.getEmpByEno(eno);
    }

    @PostMapping("/update")
    public ResponseResult updateEmp(Emp emp){
        return empServices.update(emp);
    }
}
